package com.example.ormsqlsplicingframe1.sql.impl;

import com.example.ormsqlsplicingframe1.note.JoinTransaction;
import com.example.ormsqlsplicingframe1.sql.ConditionSql;
import com.example.ormsqlsplicingframe1.sql.FieldSql;
import com.example.ormsqlsplicingframe1.sql.UpdateSql;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@JoinTransaction
@Service
public class UpdateImpl implements UpdateSql {

    // name =?,age = ?  name
private FieldsSql fieldsSql = new FieldsSql();
private ConditionSql conditionSql = new ConditionImpl();


   private String table;



    @Override
    public FieldSql fields() {
        return fieldsSql;
    }

    @Override
    public String getSql() {

         String sql = "update " + table + " set "+fieldsSql.getUpdateSql();

        final String where = conditionSql.getSql();
        if (where.length()>0){
            sql+=" where "+where;
        }

        return sql;
    }

    @Override
    public List<Object> getValues() {
        final ArrayList<Object> list = new ArrayList<>();
        list.addAll(fieldsSql.getValues());
        list.addAll(conditionSql.getValues());
        return list;
    }

    @Override
    public UpdateSql table(String table) {

        this.table = table;
        return this;
    }

    @Override
    public ConditionSql where() {
        return conditionSql;
    }
}
